package org.gfix.parser;

import org.gfix.parser.FixHandler;
import org.jmock.Expectations;
import org.jmock.integration.junit4.JMock;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(JMock.class)
public class NonValidatingFixParserTest extends BaseParserTest
{

	@Test
	public void testSimpleMessageParseOneElementOnly() throws Exception
	{
		final FixHandler handler = context.mock(FixHandler.class);			
		runTest(handler, "1=hello", new Expectations()
		{{
		    oneOf (handler).newTag(with(1), with("hello"));
		}});
	}
	
	@Test
	public void testSimpleMessageParseTwoElements() throws Exception
	{
		//"8=FIX.4.2\0019=12\00135=X\001108=30\00110=049\001"
		final FixHandler handler = context.mock(FixHandler.class);			
		runTest(handler, "1=hello\0012=world", new Expectations()
		{{
		    oneOf (handler).newTag(with(1), with("hello"));
		    oneOf (handler).newTag(with(2), with("world"));
		}});
	}
	
	@Test
	public void testSimpleMessageParseThreeElementsWithEndLimiter() throws Exception
	{
		//"8=FIX.4.2\0019=12\00135=X\001108=30\00110=049\001"
		final FixHandler handler = context.mock(FixHandler.class);			
		runTest(handler, "1=hello\0012=world\0013=of\001", new Expectations()
		{{
		    oneOf (handler).newTag(with(1), with("hello"));
		    oneOf (handler).newTag(with(2), with("world"));
		    oneOf (handler).newTag(with(3), with("of"));
		}});
	}
	
	@Test
	public void testSimpleMessageParseWithBlankValue() throws Exception
	{
		// note this test the message is technical invalid
		// even ignoring the lack of headers etc.
		//"8=FIX.4.2\0019=12\00135=X\001108=30\00110=049\001"
		final FixHandler handler = context.mock(FixHandler.class);			
		runTest(handler, "1=hello\0012=\0013=of\001", new Expectations()
		{{
		    oneOf (handler).newTag(with(1), with("hello"));
		    oneOf (handler).newTag(with(2), with(""));
		    oneOf (handler).newTag(with(3), with("of"));
		}});
	}
	
	@Test
	public void testSimpleMessageParseWithBlankAndNoEqualsValue() throws Exception
	{
		// note this test the message is technical invalid
		// even ignoring the lack of headers etc.
		//"8=FIX.4.2\0019=12\00135=X\001108=30\00110=049\001"
		final FixHandler handler = context.mock(FixHandler.class);			
		runTest(handler, "1=hello\0012\0013=of\001", new Expectations()
		{{
		    oneOf (handler).newTag(with(1), with("hello"));
		    oneOf (handler).newTag(with(2), with(""));
		    oneOf (handler).newTag(with(3), with("of"));
		}});
	}
	
	@Test
	public void testSimpleMessageParseWithEqualsInTheValue() throws Exception
	{
		// note this test the message is technical invalid
		// even ignoring the lack of headers etc.
		//"8=FIX.4.2\0019=12\00135=X\001108=30\00110=049\001"
		final FixHandler handler = context.mock(FixHandler.class);			
		runTest(handler, "1=hello\0012=world=yes\0013=of\001", new Expectations()
		{{
		    oneOf (handler).newTag(with(1), with("hello"));
		    oneOf (handler).newTag(with(2), with("world=yes"));
		    oneOf (handler).newTag(with(3), with("of"));
		}});
	}
	
	@Test
	public void testRepeatingTags() throws Exception
	{
		// needed for repeating group support
		// note this test the message is technical invalid
		// even ignoring the lack of headers etc.
		//"8=FIX.4.2\0019=12\00135=X\001108=30\00110=049\001"
		final FixHandler handler = context.mock(FixHandler.class);			
		runTest(handler, "1=hello\0011=world\0011=of\001", new Expectations()
		{{
		    oneOf (handler).newTag(with(1), with("hello"));
		    oneOf (handler).newTag(with(1), with("world"));
		    oneOf (handler).newTag(with(1), with("of"));
		}});
	}
	

	@Test
	public void testSimpleLoginMessage() throws Exception
	{
		final FixHandler handler = context.mock(FixHandler.class);			
		runTest(handler, "8=FIX.4.2\0019=12\00135=X\001108=30\00110=049\001", new Expectations()
		{{
		    oneOf (handler).newTag(with(8), with("FIX.4.2"));
		    oneOf (handler).newTag(with(9), with("12"));
		    oneOf (handler).newTag(with(35), with("X"));
		    oneOf (handler).newTag(with(108), with("30"));
		    oneOf (handler).newTag(with(10), with("049"));
		}});
	}
	
	/**
	 * Issue #2
	 * @throws Exception
	 */
	@Test
	public void testSimpleMessageParseInvalidTagNumber() throws Exception
	{
		final FixHandler handler = context.mock(FixHandler.class);			
		runTest(handler, "1=hello\001 =blob\001", new Expectations()
		{{
		    oneOf (handler).newTag(with(1), with("hello"));
		    oneOf (handler).error(with(any(FixParseFault.class)));
		}});
	}
	/**
	 * Issue #2
	 * @throws Exception
	 */
	@Test
	public void testSimpleMessageParseInvalidTagNumberValue() throws Exception
	{
		final FixHandler handler = context.mock(FixHandler.class);			
		runTest(handler, "1=hello\001A=blob\001", new Expectations()
		{{
		    oneOf (handler).newTag(with(1), with("hello"));
		    oneOf (handler).error(with(any(FixParseFault.class)));
		}});
	}
}
